package com.fox0g.controller;



import com.fox0g.mapper.*;
import com.fox0g.netty.NettyServerHandler;


import com.fox0g.pojo.*;
import com.fox0g.service.*;
import io.netty.util.CharsetUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @program: agricultureControlSystem
 * @Date: 2022-07-27 10:57
 * @author: Fox0g
 * @description:
 */
@RestController
@CrossOrigin
public class MyHtmlController {

    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    FarmService farmService;


    @Autowired
    YsjrepeatService ysjrepeatService;


    @Autowired
    FarmMapper farmMapper;

    @Autowired
    QueuecountMapper queuecountMapper;

    @Autowired
    FreshfoodMapper freshfoodMapper;


    @Autowired
    SharebikeMapper sharebikeMapper;
    @Autowired
    YsjrepeatMapper ysjrepeatMapper;



    @Autowired
    TstateMapper tstateMapper;

    @Autowired
    ProfitMapper profitMapper;


    @Autowired
    VirusMapper virusMapper;

    @Autowired
    GlsMapper glsMapper;


    @Autowired
    InoroutMapper inoroutMapper;

    @Autowired
    VirusService virusService;

    @Autowired
    PatienttMapper patienttMapper;




    @RequestMapping("/list")
    public String json(@RequestBody Ysjrepeat ysjrepeat){
        System.out.println(ysjrepeat);
        ysjrepeatMapper.insert(ysjrepeat);
        return "ok";
    }


    //病人进出隔离室
    @RequestMapping("/inandoutlist")
    public String json(@RequestBody Inorout inorout){
        System.out.println(inorout);
        inoroutMapper.insert(inorout);
        return "ok";
    }



//    @GetMapping("/inoutlist/{page}")
//    public Result page(@PathVariable Integer page,@Param("begin") String begin, @Param("end") String end){
//        System.out.println(page);
//        PageBean pageBean = virusService.page(page,begin,end);
//        System.out.println(pageBean);
//        return Result.success(pageBean);
//    }

    //查询历史记录
//    @GetMapping("/ysjlist/{page}")
//    public Result page(@PathVariable Integer page){
//        System.out.println(page);
//        PageBean pageBean = ysjrepeatService.page(page);
//        System.out.println(pageBean);
//        return Result.success(pageBean);
//    }

//    @GetMapping("/ysjwarnlist/{page}")
//    public Result warnpage(@PathVariable Integer page){
//        System.out.println(page);
//        PageBean pageBean = ysjrepeatService.warnpage(page);
//        System.out.println(pageBean);
//        return Result.success(pageBean);
//    }

    @GetMapping("/list1")
    public Result findALLDataByDate(@Param("begin") String begin, @Param("end") String end){
        System.out.println("begin->"+begin);
        System.out.println("end->"+end);
//        List<Freshfood> freshfoodList = freshfoodMapper.selectbydate(begin,end);
        List<Freshfood> freshfoodList=farmService.getByTime(begin,end);
        return Result.success(freshfoodList);
    }

    //按温度查询
    @GetMapping("/listbytstate")
    public Result findALLByTstate(@Param("begin") String begin, @Param("end") String end){
        List<Farm> farmList = farmMapper.selectbytstate(begin,end);
        return Result.success(farmList);
    }

    //按湿度查询
    @GetMapping("/listbyhstate")
    public Result findALLByHstate(@Param("begin") String begin, @Param("end") String end){
        List<Farm> farmList = farmMapper.selectbyhstate(begin,end);
        return Result.success(farmList);
    }


    //按光照强度查询
    @GetMapping("/listbylsstate")
    public Result findALLByLsstate(@Param("begin") String begin, @Param("end") String end){
        List<Farm> farmList = farmMapper.selectbylsstate(begin,end);
        return Result.success(farmList);
    }




    @GetMapping("/ysjlist")
    public Result findYsjALLData(){
        List<Ysjrepeat> ysjrepeatList = ysjrepeatMapper.YsjallData();
        return Result.success(ysjrepeatList);
    }


    @GetMapping("/Tlist")
    public Result findTALLData(){
        List<Tstate> tstateList =tstateMapper.tAllData();
        return Result.success(tstateList);
    }

    //收益
    @GetMapping("/Profitlist")
    public Result findProfitALLData(){
        List<Profit> profitList = profitMapper.profitAllData();
        return Result.success(profitList);
    }

    //病毒含量
    @GetMapping("/viruslist")
    public Result virusfindTALLData(){
        List<Virus> virusList = virusMapper.virusAllData();
        return Result.success(virusList);
    }

    //隔离室状态
    @GetMapping("/glslist")
    public Result glsfindTALLData(@Param("begin") String begin, @Param("end") String end){
        List<Gls> glsList = glsMapper.glsAllData(begin,end);
        return Result.success(glsList);

    }


    //病人进出
    @GetMapping("/inoutlist/")
    public Result page(@Param("begin") String begin, @Param("end") String end){
        List<Inorout> inorouts = inoroutMapper.AllData(begin,end);
        return Result.success(inorouts);
    }


    //病人体温折线图渲染
    @GetMapping("/patientlist/")
    public Result alldatebypatientid(@Param("patientid") String patientid){
        List<Patientt> patienttList = patienttMapper.selectbypatientid(patientid);
        return Result.success(patienttList);
    }

    //病人体温历史数据
    @GetMapping("/patientbydatelist/")
    public Result alldatebypatientidbydate(@Param("patientid") String patientid,@Param("begin") String begin,@Param("end") String end){
        List<Patientt> patienttList = patienttMapper.AllData(patientid,begin,end);
        return Result.success(patienttList);
    }





    /**
     * @param message 从前端收到信息向服务器发送的信息
     */
    @GetMapping("/sendMessageToMyself/{message}")
    public void sendMessageToMyself( @PathVariable String message) throws InterruptedException {
        System.out.println(message);
        System.out.println("****主动发送消息****");
        Thread.sleep(100+new Random().nextInt(1000));
        String s[] = message.split(" ");
        Patientt patientt = new Patientt();
        patientt.setPatientid(s[0]);
        patientt.setTw(s[1]);
        LocalTime now = LocalDateTime.now().toLocalTime();
        patientt.setCreateTime(now);
        patienttMapper.insert(patientt);
        }








//
//    //向服务器发数据
//    public void sendMessageToMyself(@RequestBody String msg) throws IOException {
//        Socket socket = new Socket("localhost", 10086);
//        OutputStream outputStream = socket.getOutputStream();
//        outputStream.write(msg.getBytes(CharsetUtil.UTF_8));
//        outputStream.flush();
//        socket.shutdownOutput();
//    }

    @GetMapping("input")
    public Result read() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\javacode\\echartsave\\echart.txt"));
        String line;
        while((line = br.readLine())!=null){
            String[] s = line.split(" ");
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");
            System.out.println(Integer.parseInt(s[0]) + " " +Integer.parseInt(s[1])+ " " +LocalTime.parse(s[2],fmt));
            queuecountMapper.inputupdate(Integer.parseInt(s[0]),Integer.parseInt(s[1]),LocalTime.parse(s[2],fmt));
        }
        return Result.success();
    }
    @PostMapping("/saveOption")
    public Result saveOption(@RequestBody String json) throws IOException {
        BufferedWriter bf = new BufferedWriter(new FileWriter("D:\\javacode\\echartsave\\echart.txt"));
        List<Queuecount> queuecountList = queuecountMapper.allData();
        for (Queuecount queuecount : queuecountList) {
            System.out.println(queuecount.getId() + " " +queuecount.getNum() + " " + queuecount.getCreateTime());
            String time = queuecount.getCreateTime().toString();
            if(time.length() < 8){
                time = time + ":00";
            }
            System.out.println(queuecount.getId() + " " +queuecount.getNum() + " " + time);
            bf.write(queuecount.getId() + " " +queuecount.getNum() + " " + time);
            bf.newLine();
        }
        bf.close();
        return Result.success();
    }

    /**
     * @param message 从前端收到信息向服务器发送的信息
     */
    @GetMapping("/sendMessageTo/{message}")
    public void sendMessageTo( @PathVariable String message) throws InterruptedException {
        System.out.println(message);
        System.out.println("****主动发送消息****");
        Thread.sleep(100 + new Random().nextInt(1000));
        nettyServerHandler.doAllReply(message);

    }


}
